package com.free.ppcar.business;

import com.free.ppcar.common.UrlRequestInfo;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ValidateService {

    /**
     * 是否停车页面
     * @param url
     * @return
     */
    public static UrlRequestInfo getUrlInfo(String url){
        UrlRequestInfo u = new UrlRequestInfo();
        u.setUrl(url);
        Map<String, String> params = new HashMap<>();
        if (url == null) {
            return null;
        }
        if (url.contains("?")){
            String[] split = url.split("\\?");
            if (split.length == 2){
                u.setUri(split[0]);

                String queryString = split[1];
                u.setQueryString(queryString);
                String[] paramString = queryString.split("&");
                // 封装参数
                for (String param : paramString) {
                    String[] split1 = param.split("=");
                    if (split1.length == 2){
                        params.put(split1[0], split1[1]);
                    }
                }
                u.setParam(params);
            }
        }else {
            u.setUrl(url);
        }
        return u;
    }

    public static boolean isPayUrl(String url){
        if (url == null || url.isEmpty()){
            return false;
        }
        if (url.startsWith("http://vip.lcmsh.com/car_park_normal")){
            UrlRequestInfo urlInfo = getUrlInfo(url);
            if (urlInfo == null){
                return false;
            }
            Map<String, String> param = urlInfo.getParam();
            if (param == null || param.keySet().size() != 1){
                return false;
            }
            if (param.containsKey("car_no")){
                return true;
            }
        }
        return false;
    }
}
